package com.leyou.auth.config;

import com.leyou.common.auth.utils.RsaUtils;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.xml.soap.SAAJResult;
import java.security.PrivateKey;
import java.security.PublicKey;

@Data
@Slf4j
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {

    private String pubKeyPath;
    private String priKeyPath;
    private PrivateKey privateKey;
    private PublicKey publicKey;
    private UserTokenProperties user=new UserTokenProperties();
    private AppTokenProperties app=new AppTokenProperties();


    @Data
    public class UserTokenProperties{

        private int expire;
        private String cookieName;
        private String cookieValue;
        private String cookieDomain;
        private int minRefreshInterval;
    }

    @Data
    public class AppTokenProperties{

        private int expire;
        private Long id;
        private String headerName;
        private String secret;

    }


    @Override
    public void afterPropertiesSet() throws Exception {

        try {
            this.privateKey=RsaUtils.getPrivateKey(priKeyPath);
            this.publicKey=RsaUtils.getPublicKey(pubKeyPath);
        } catch (Exception e) {
            throw new LyException(ExceptionEnum.GET_KEY_ERROR);
        }

    }
}